//
//  Fader
//
//  Created by Wonil Suh on 2009-07-12.
//  Copyright (c) 2009 secret toilet. All rights reserved.
//  Contact: wosuh@secretToilet.com
//  
//  DESCRIPTION
//  

package com.wonilsuh.display {
	
	// import
	import flash.display.DisplayObject;
	
	import flash.events.IEventDispatcher;
	import flash.events.EventDispatcher;
	import flash.events.Event;
	
	import fl.transitions.Tween;
	import fl.transitions.easing.*;
	import fl.transitions.TweenEvent;
	
	public class Fader extends Object implements IFader, IEventDispatcher {

		// properties
		private var fader_targetDObj:DisplayObject;
		private var fader_twnA:Tween;
		private var fader_twnEasingFn:Function;
		private var hideAfterFO:Boolean = true;
		
		private var fader_eventDispatcher:EventDispatcher;
		
		// timeline elements
		
		// constants
		public static const FADER_FADE_COMPLETE:String = "FADER_FADE_COMPLETE";
		
		//----------------------------------------
		// constructor
		public function Fader(pTarget:DisplayObject) {
			//trace("Fader has been constructed.");
			fader_targetDObj = pTarget;
			
			setEasingFunction(Strong.easeOut);
			
			//DecoratedDispatcher();
			fader_eventDispatcher = new EventDispatcher(this);
		}

		//----------------------------------------
		// init methods
		
		//----------------------------------------
		// methods
		
		//----------------------------------------
		// getters and setters
		
		//----------------------------------------
		// interface implementations
		
		//----------------------------------------
		// interface implementations: com.wonilsuh.display.IFader
		
		public function setEasingFunction(pFn:Function):void {
			//trace(".()...");
			fader_twnEasingFn = pFn;
		}
		
		public function fade(_from:Number=0,_to:Number=1,_duration:Number=1, pHideAfterFO:Boolean=true):void {
			if(fader_twnA) {
				fader_twnA.stop();
			}
			fader_twnA = new Tween(fader_targetDObj, "alpha", fader_twnEasingFn, _from, _to, _duration, true);
			fader_twnA.addEventListener(TweenEvent.MOTION_FINISH, onFadeFinish);
		}
		
		public function onFadeFinish(e:TweenEvent):void {
			//trace("Fader.onFadeFinish()...");
			fader_twnA.removeEventListener(TweenEvent.MOTION_FINISH, onFadeFinish);
						
			dispatchEvent(new Event(Fader.FADER_FADE_COMPLETE));
			
			if(fader_targetDObj.alpha==0 && hideAfterFO) {
				fader_targetDObj.visible = false;
			}
		}
				
		public function fadeTo(_to:Number=1,_duration:Number=1):void {
			fade(fader_targetDObj.alpha, _to, _duration);
		}
		
		public function fadeIn(_duration:Number=1):void {
			if(!fader_targetDObj.visible) {
				fader_targetDObj.visible = true;
			}
			fade(fader_targetDObj.alpha, 1, _duration);
		}
		
		public function fadeOut(_duration:Number=1, pHideAfterFO:Boolean=true):void {
			fade(fader_targetDObj.alpha, 0, _duration, hideAfterFO);
		}
		
		//----------------------------------------
		// interface implementations: flash.event.EventDispatcher
		
		public function DecoratedDispatcher() {
			//trace("Fader.DecorateDispatcher()...");
			fader_eventDispatcher = new EventDispatcher(this);
		}

		public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void{
			fader_eventDispatcher.addEventListener(type, listener, useCapture, priority);
		}

		public function dispatchEvent(evt:Event):Boolean {
			//trace("Fader.dispatchEvent()...");
			return fader_eventDispatcher.dispatchEvent(evt);
		}
		
		public function hasEventListener(type:String):Boolean{
			return fader_eventDispatcher.hasEventListener(type);
		}
		
		public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void{
			fader_eventDispatcher.removeEventListener(type, listener, useCapture);
		}
		
		public function willTrigger(type:String):Boolean {
			return fader_eventDispatcher.willTrigger(type);
		}
		
	}
}